package com.fingard.dsp.bank.directConfig;

import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class FtpSet {
	public String serverIP;
	public int port = 0;
	public String userName;
	public String userPassword;
	public String upDir;//上传目录
	public String downDir;//下载目录
	public String errDir;//错误目录
	public String charset;
	public int timeout;//超时时间
	public String sshKeyPath;//ssh证书
	public String sshKeyPassword;//ssh证书密码
	public String ftpType;
	
	private HashMap<String,String> upDirMap;
	private HashMap<String,String> downDirMap;
	private HashMap<String,String> errDirMap;
	private HashMap<String,String> userNameMap;
	private HashMap<String,String> userPasswordMap;
	private HashMap<String,String> sshKeyPathMap;
	
	public void fromXElement(XElement p_xe) throws Exception{
		serverIP = p_xe.getSingleTextNotNull("serverIP");
		if(StringHelper.hasAnyChar(p_xe.getSingleTextNotNull("port"))){
			port = Integer.parseInt(p_xe.getSingleTextNotNull("port"));
		}
		userName = p_xe.getSingleTextNotNull("userName");
		userPassword = p_xe.getSingleTextNotNull("userPassword");
		
		upDir = p_xe.getSingleTextNotNull("upDir");
		downDir = p_xe.getSingleTextNotNull("downDir");
		errDir = p_xe.getSingleTextNotNull("errDir");
		charset = p_xe.getSingleTextNotNull("charset");
		if (StringHelper.isNullOrEmpty(charset)) {
            charset = "UTF-8";
        }
		sshKeyPath =p_xe.getSingleTextNotNull("sshKeyPath");
		sshKeyPassword=p_xe.getSingleTextNotNull("sshKeyPassword");
		ftpType = p_xe.getSingleTextNotNull("ftpType").toLowerCase();
		
		upDirMap = getForMap(p_xe, "upDir");
		downDirMap = getForMap(p_xe, "downDir");
		errDirMap = getForMap(p_xe, "errDir");
		userNameMap = getForMap(p_xe, "userName");
		userPasswordMap = getForMap(p_xe, "userPassword");
		sshKeyPathMap = getForMap(p_xe,"sshKeyPath");
		if(StringHelper.hasAnyChar(p_xe.getSingleTextNotNull("timeout"))){
			timeout = Integer.parseInt(p_xe.getSingleTextNotNull("timeout"));
		}
		
	}
	
	/**按for属性获取上传目录*/
	public String getUpDir(String p_for){
		if(upDirMap != null && upDirMap.containsKey(p_for)){
			return trimEndFileSpForFtp(upDirMap.get(p_for));
		}
		return null;
	}
	
	/**按for属性获取下载目录*/
	public String getDownDir(String p_for){
		if(downDirMap != null && downDirMap.containsKey(p_for)){
			return trimEndFileSpForFtp(downDirMap.get(p_for));
		}
		return null;
	}
	/**遍历DownDir*/
	public Set<String> listDownDir(){
		if(downDirMap != null ){
			return downDirMap.keySet();
		}
		return null;
	}
	/**按for属性获取错误目录*/
	public String getErrDir(String p_for){
		if(errDirMap != null && errDirMap.containsKey(p_for)){
			return trimEndFileSpForFtp(errDirMap.get(p_for));
		}
		return null;
	}
	
	/**按for属性获取用户名*/
	public String getUserName(String p_for){
		if(userNameMap != null && userNameMap.containsKey(p_for)){
			return userNameMap.get(p_for);
		}
		return null;
	}
	/**按for属性获取ssh地址*/
	public String getSshKeyPath(String p_for){
		if(sshKeyPathMap != null && sshKeyPathMap.containsKey(p_for)){
			return sshKeyPathMap.get(p_for);
		}
		return null;
	}
	
	/**按for属性获取用户密码*/
	public String getUserPassword(String p_for){
		if(userPasswordMap != null && userPasswordMap.containsKey(p_for)){
			return userPasswordMap.get(p_for);
		}
		return null;
	}
	
	private HashMap<String,String> getForMap(XElement p_xe, String p_nodeName){
		HashMap<String,String> retMap = null;
		ArrayList<XElement> listXe = p_xe.getChildList(p_nodeName);
		if(listXe != null && listXe.size() > 0){
			retMap = new HashMap<String,String>();
			for(int i=0;i<listXe.size();i++){
				String eachFor = listXe.get(i).getAttrValue("for");
				if(eachFor == null){
					eachFor = "";
				}
				retMap.put(eachFor, listXe.get(i).innerText);
			}
		}
		return retMap;
	}
	
	private String trimEndFileSpForFtp(String pVal){
		if("/".equals(pVal.trim())){//只有根目录
    		return pVal.trim();
    	}
    	if("\\".equals(pVal.trim())){//只有根目录
    		return pVal.trim();
    	}
		return StringHelper.trimEndFileSp(pVal);
	}
	
	public FtpSet clone(){
		FtpSet retFtpSet = new FtpSet();
		retFtpSet.serverIP = this.serverIP;
		
		retFtpSet.port = this.port;
		retFtpSet.userName = this.userName;
		retFtpSet.userPassword = this.userPassword;
		retFtpSet.upDir = this.upDir;
		retFtpSet.downDir = this.downDir;
		retFtpSet.errDir = this.errDir;
		retFtpSet.charset = this.charset;
		retFtpSet.timeout = this.timeout;
		retFtpSet.sshKeyPath = this.sshKeyPath;
		retFtpSet.ftpType = this.ftpType;
		
		return retFtpSet;
	}
	
}
